## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
## Registered S3 method overwritten by 'rvest':
## method from
## read_xml.response xml2
## ── Attaching packages ──────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.1 ✔ dplyr 0.8.0.1
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ─────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
PID county state area poptotal popdensity popwhite popblack 1 561 ADAMS IL 0.052 66090 1270.9615 63917 1702 2 562 ALEXANDER IL 0.014 10626 759.0000 7054 3496 3 563 BOND IL 0.022 14991 681.4091 14477 429 4 564 BOONE IL 0.017 30806 1812.1176 29344 127 5 565 BROWN IL 0.018 5836 324.2222 5264 547 6 566 BUREAU IL 0.050 35688 713.7600 35157 50 popamerindian popasian popother percwhite percblack percamerindan 1 98 249 124 96.71206 2.5752761 0.1482826 2 19 48 9 66.38434 32.9004329 0.1788067 3 35 16 34 96.57128 2.8617170 0.2334734 4 46 150 1139 95.25417 0.4122574 0.1493216 5 14 5 6 90.19877 9.3728581 0.2398903 6 65 195 221 98.51210 0.1401031 0.1821340 percasian percother popadults perchsd percollege percprof 1 0.37675897 0.18762294 43298 75.10740 19.63139 4.355859 2 0.45172219 0.08469791 6724 59.72635 11.24331 2.870315 3 0.10673071 0.22680275 9669 69.33499 17.03382 4.488572 4 0.48691813 3.69733169 19272 75.47219 17.27895 4.197800 5 0.08567512 0.10281014 3979 68.86152 14.47600 3.367680 6 0.54640215 0.61925577 23444 76.62941 18.90462 3.275891 poppovertyknown percpovertyknown percbelowpoverty percchildbelowpovert 1 63628 96.27478 13.151443 18.01172 2 10529 99.08714 32.244278 45.82651 3 14235 94.95697 12.068844 14.03606 4 30337 98.47757 7.209019 11.17954 5 4815 82.50514 13.520249 13.02289 6 35107 98.37200 10.399635 14.15882 percadultpoverty percelderlypoverty inmetro category 1 11.009776 12.443812 0 AAR 2 27.385647 25.228976 0 LHR 3 10.852090 12.697410 0 AAR 4 5.536013 6.217047 1 ALU 5 11.143211 19.200000 0 AAR 6 8.179287 11.008586 0 AAR
popdensity by state.county by state.
ggplot(data = midwest) from above.
alpha instead.
colors().
ggplot(midwest) +
geom_point(aes(x = popdensity, y = percollege, color = state)) +
geom_smooth(aes(x = popdensity, y = percollege, color = state),
se = FALSE)ggplot(midwest,
aes(x = popdensity, y = percollege, color = state)) +
geom_point() +
geom_smooth(se = FALSE)ggplot(midwest,
aes(x = popdensity, y = percollege, color = state)) +
geom_point() +
geom_smooth(se = FALSE) +
theme_bw()p1 +
labs(title = "Percent College Educated by Population Density",
subtitle = "County level data for five midwest states")scale_x_continuous - Bad Practice## Warning: Removed 16 rows containing non-finite values (stat_smooth).
## Warning: Removed 16 rows containing missing values (geom_point).
## Parsed with column specification:
## cols(
## Film = col_character(),
## Chapter = col_character(),
## Character = col_character(),
## Race = col_character(),
## Words = col_double()
## )
## # A tibble: 6 x 5
## Film Chapter Character Race Words
## <chr> <chr> <chr> <chr> <dbl>
## 1 The Fellowship Of The Ring 01: Prologue Bilbo Hobbit 4
## 2 The Fellowship Of The Ring 01: Prologue Elrond Elf 5
## 3 The Fellowship Of The Ring 01: Prologue Galadriel Elf 460
## 4 The Fellowship Of The Ring 02: Concerning Hobbits Bilbo Hobbit 214
## 5 The Fellowship Of The Ring 03: The Shire Bilbo Hobbit 70
## 6 The Fellowship Of The Ring 03: The Shire Frodo Hobbit 128
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
geom_density to create a density plot for Words for each film.geom_boxplot, create boxplots with Words as the y variable and Film as the x variable. Bonus: facet this plot by the variable Race. Bonus2: Zoom in on the bulk of the data.ggplot(lotr, aes(x = Film, y = Words)) +
geom_boxplot() +
facet_wrap(~ Race) +
theme_bw() +
theme(axis.text.x = element_text(angle = 90))coord_flip is betterggplot(lotr, aes(x = Film, y = Words)) +
geom_boxplot() +
facet_wrap(~ Race) +
theme_bw() +
coord_flip()ggplot(lotr, aes(x = Race)) +
geom_bar(aes(fill = Film), position = 'fill') +
theme_bw() +
ylab("Proportion")ggplot(lotr, aes(x = Race)) +
geom_bar(aes(fill = Film), position = 'fill') +
theme_bw() +
ylab("Proportion") +
scale_fill_viridis(option = 'cividis', discrete = TRUE)partyid.marital to the bar chart created in step 1. Do you prefer a stacked or dodged version?starwars data, create a static ggplot and use the ggplotly function to turn it interactive.one_plot <- function(d) {
plot_ly(d, x = ~Words, width = 900) %>%
add_histogram() %>%
add_annotations(
~unique(Film), x = 0.5, y = 1,
xref = "paper", yref = "paper", showarrow = FALSE
)
}
plotly_out <- lotr %>%
split(.$Film) %>%
lapply(one_plot) %>%
subplot(nrows = 1, shareX = TRUE, titleX = FALSE) %>%
hide_legend()## number of diamonds by cut and clarity (n)
lotr_count <- count(lotr, Race, Film)
## number of diamonds by cut (nn)
lotr_prop <- left_join(lotr_count, count(lotr_count, Race, wt = n),
by = 'Race')
plotly_out <-lotr_prop %>%
mutate(prop = n.x / n.y) %>%
plot_ly(x = ~Race, y = ~prop, color = ~Film, width = 900) %>%
add_bars() %>%
layout(barmode = "stack")gss_cat data, create a histrogram for the tvhours variable.gss_cat data, create a bar chart showing the partyid variable by the marital status.gss_cat data, create a scatterplot showing the age and tvhours variables.highcharter## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
## # A tibble: 6 x 3
## Film Race n
## <chr> <chr> <int>
## 1 The Fellowship Of The Ring Dwarf 11
## 2 The Fellowship Of The Ring Elf 31
## 3 The Fellowship Of The Ring Hobbit 103
## 4 The Fellowship Of The Ring Man 40
## 5 The Fellowship Of The Ring Orc 3
## 6 The Fellowship Of The Ring Wizard 29
hchart functionhcharthchart function, create a bar chart or histogram with the gss_cat data.hchart function, create a scatterplot with the gss_cat data.hc <- highchart() %>%
hc_xAxis(categories = lotr_count$Race) %>%
hc_add_series(name = 'The Fellowship Of The Ring',
data = filter(lotr_count, Film == 'The Fellowship Of The Ring')$n) %>%
hc_add_series(name = 'The Two Towers',
data = filter(lotr_count, Film == 'The Two Towers')$n) %>%
hc_add_series(name = 'The Return Of The King',
data = filter(lotr_count, Film == 'The Return Of The King')$n)hc <- hc %>%
hc_title(text = 'Number of Words Spoken in Lord of the Rings Films',
align = 'left') %>%
hc_subtitle(text = 'Broken down by <i>Film</i> and <b>Race</b>',
align = 'left') %>%
hc_legend(align = 'right', verticalAlign = 'top', layout = 'vertical',
x = 0, y = 80) %>%
hc_exporting(enabled = TRUE)gss_cat data.library(gganimate)
ggplot(storms, aes(x = pressure, y = wind, color = status)) +
geom_point(show.legend = FALSE) +
xlab("Pressure") +
ylab("Wind Speed (MPH)") +
facet_wrap(~status) +
theme_bw(base_size = 14) +
labs(title = 'Year: {frame_time}') +
transition_time(as.integer(year)) +
ease_aes('linear')